/**
 * Created by thinkpad on 2017-05-31.
 */

XBox.define("grid.crud", {
    title: "popup列表示例",
    templates: {
        main: ["<div>" +
        "            <div class='grid'></div>" +
        "</div>"],
    },
    elements: {
        grid: ".grid"
    },
    afterRender: function () {
        var that = this;
        var crudServiceBaseUrl = "webapi/v1";
        dataSource = new kendo.data.DataSource({
            transport: {
                read: { url: crudServiceBaseUrl + "/Test/Get", },
                update: { url: crudServiceBaseUrl + "/Test/Update2", type: "post" },
                destroy: { url: crudServiceBaseUrl + "/Test/Delete2", type: "post" },
                create: { url: crudServiceBaseUrl + "/Test/Create2", type: "post" },
                parameterMap: function (options, operation) {
                    debugger;
                    if (operation !== "read" && options.models) {
                        debugger;
                        return options.models[0];
                    } else {
                        return options;
                    }
                }
            },
            serverPaging: true,
            serverSorting: true,
            serverFiltering: true,
            serverGrouping: true,
            serverAggregates: true,
            batch: false,
            pageSize: 10,
            schema: {
                total: "Total",
                data: "Data",
                model: {
                    id: "ProductID",
                    fields: {
                        ProductID: { editable: false, nullable: true },
                        ProductName: { validation: { required: true } },
                        UnitPrice: { type: "number", validation: { required: true, min: 1 } },
                        Discontinued: { type: "boolean" },
                        UnitsInStock: { type: "number", validation: { min: 0, required: true } }
                    }
                }
            }
        });

        this.controls.grid.kendoGrid({
            dataSource: dataSource,
            pageable: {
                pageSize: 10,
                refresh: true
            },
            sortable: true,
            groupable: true,
            filterable: {
                operators: {
                    string: {
                        contains: "模糊匹配"
                    },
                    number: {
                        gte: "大于等于",
                        gt: "大于",
                        lte: "小于等于",
                        lt: "小于",
                    }
                }
            },
            height: 500,
            excel: {
                allPages: true,
                fileName: "Products.xlsx"
            },
            toolbar: [
                { name: "create", text: "增" },
                { name: "excel" },
                { name: "pdf" }
            ],
            columns: [
                {
                    field: "ProductName",
                    title: "名称",
                    filterable: {
                        extra: false,
                        ui: function (element) {
                            element.kendoAutoComplete({
                                delay: 0,
                                filter: "contains",
                                dataSource: dataSource,
                                dataTextField: "ProductName"
                            });
                        }
                    }
                },
                { field: "UnitPrice", title: "单价", format: "{0:c}", width: "80px" },
                { field: "UnitsInStock", title: "库存数量", width: "110px" },
                { field: "Discontinued", title: "折扣", width: "80px" },
                {
                    command: [{ name: "edit", text: "改" }, { name: "destroy", text: "删" }],
                    title: "操作",
                    width: "250px"
                }],
            editable: "inline"
        }).data("kendoGrid");

        //jQuery("#grid")
        //    .kendoGrid({
        //        "columns": [
        //        {
        //            "title": "Product ID",
        //            "width": "100px",
        //            "field": "ProductID",
        //            "filterable": { "messages": { "info": "显示符合以下条件的行", "isTrue": "为真", "isFalse": "为假", "filter": "过滤", "clear": "清除过滤", "and": "并且", "or": "或者", "selectValue": "-选择值-", "operator": "接线员", "value": "值", "cancel": "取消" }, "operators": { "string": { "eq": "等于", "neq": "不等于", "startswith": "开始于", "endswith": "结束于", "contains": "包含", "doesnotcontain": "不包含" }, "number": { "eq": "等于", "neq": "不等于", "gte": "大于或等于", "gt": "大于", "lte": "小于或等于", "lt": "小于" }, "date": { "eq": "等于", "neq": "不等于", "gte": "大于或等于", "gt": "大于", "lte": "小于或等于", "lt": "小于" }, "enums": { "eq": "等于", "neq": "不等于" } } },
        //            "encoded": true
        //        }, {
        //            "title": "Product Name",
        //            "field": "ProductName",
        //            "filterable": { "messages": { "info": "显示符合以下条件的行", "isTrue": "为真", "isFalse": "为假", "filter": "过滤", "clear": "清除过滤", "and": "并且", "or": "或者", "selectValue": "-选择值-", "operator": "接线员", "value": "值", "cancel": "取消" }, "operators": { "string": { "eq": "等于", "neq": "不等于", "startswith": "开始于", "endswith": "结束于", "contains": "包含", "doesnotcontain": "不包含" }, "number": { "eq": "等于", "neq": "不等于", "gte": "大于或等于", "gt": "大于", "lte": "小于或等于", "lt": "小于" }, "date": { "eq": "等于", "neq": "不等于", "gte": "大于或等于", "gt": "大于", "lte": "小于或等于", "lt": "小于" }, "enums": { "eq": "等于", "neq": "不等于" } } },
        //            "encoded": true,
        //            "editor": "\u003cinput class=\"k-textbox\" id=\"ProductName\" name=\"ProductName\" /\u003e\u003cspan class=\"field-validation-valid\" data-valmsg-for=\"ProductName\" data-valmsg-replace=\"true\"\u003e\u003c/span\u003e"
        //        }, {
        //            "title": "Units In Stock",
        //            "width": "250px",
        //            "field": "UnitsInStock",
        //            "filterable": { "messages": { "info": "显示符合以下条件的行", "isTrue": "为真", "isFalse": "为假", "filter": "过滤", "clear": "清除过滤", "and": "并且", "or": "或者", "selectValue": "-选择值-", "operator": "接线员", "value": "值", "cancel": "取消" }, "operators": { "string": { "eq": "等于", "neq": "不等于", "startswith": "开始于", "endswith": "结束于", "contains": "包含", "doesnotcontain": "不包含" }, "number": { "eq": "等于", "neq": "不等于", "gte": "大于或等于", "gt": "大于", "lte": "小于或等于", "lt": "小于" }, "date": { "eq": "等于", "neq": "不等于", "gte": "大于或等于", "gt": "大于", "lte": "小于或等于", "lt": "小于" }, "enums": { "eq": "等于", "neq": "不等于" } } },
        //            "encoded": true,
        //            "editor": "\u003cinput class=\"k-textbox\" data-val=\"true\" data-val-number=\"The field UnitsInStock must be a number.\" id=\"UnitsInStock\" name=\"UnitsInStock\" /\u003e\u003cspan class=\"field-validation-valid\" data-valmsg-for=\"UnitsInStock\" data-valmsg-replace=\"true\"\u003e\u003c/span\u003e"
        //        }, {
        //            "title": "Commands",
        //            "width": "200px",
        //            "command": [{ "name": "edit", "buttonType": "ImageAndText", "text": { "cancel": "取消", "update": "更新", "edit": "编辑" } }, { "name": "destroy", "buttonType": "ImageAndText", "text": "删除" }]
        //        }
        //        ],
        //        "pageable": {
        //            "buttonCount": 10,
        //            "messages": { "display": "显示条目 {0} - {1} 共 {2}", "empty": "没有可显示的记录。", "page": "页", "of": "共 {0}", "first": "首页", "previous": "前页", "next": "后页", "last": "末页", "refresh": "刷新", "morePages": "更多页" }
        //        },
        //        "scrollable": false,
        //        "editable": {
        //            "confirmation": "确定要删除吗？",
        //            "confirmDelete": "删除",
        //            "cancelDelete": "取消",
        //            "mode": "inline",
        //            "create": true,
        //            "update": true,
        //            "destroy": true
        //        },
        //        "toolbar": { "command": [{ "name": null, "buttonType": "ImageAndText", "text": "插入" }] },
        //        "dataSource": {
        //            "type": (function() {
        //                if (kendo.data.transports['webapi']) {
        //                    return 'webapi';
        //                } else {
        //                    throw new Error('The kendo.aspnetmvc.min.js script is not included.');
        //                }
        //            })(),
        //            "transport": {
        //                "read": { "url": "/api/Products" },
        //                "prefix": "",
        //                "update": { "url": "/api/Products/{0}" },
        //                "create": { "url": "/api/Products" },
        //                "destroy": { "url": "/api/Products/{0}" },
        //                "idField": "ProductID"
        //            },
        //            "pageSize": 10,
        //            "page": 1,
        //            "total": 0,
        //            "serverPaging": true,
        //            "serverSorting": true,
        //            "serverFiltering": true,
        //            "serverGrouping": true,
        //            "serverAggregates": true,
        //            "filter": [],
        //            "schema": {
        //                "data": "Data",
        //                "total": "Total",
        //                "errors": "Errors",
        //                "model": { "id": "ProductID", "fields": { "ProductID": { "editable": false, "type": "number" }, "ProductName": { "type": "string" }, "SupplierID": { "type": "number", "defaultValue": null }, "CategoryID": { "type": "number", "defaultValue": null }, "QuantityPerUnit": { "type": "string" }, "UnitPrice": { "type": "number", "defaultValue": null }, "UnitsInStock": { "type": "number", "defaultValue": null }, "UnitsOnOrder": { "type": "number", "defaultValue": null }, "ReorderLevel": { "type": "number", "defaultValue": null }, "Discontinued": { "type": "boolean" } } }
        //            }
        //        }
        //    });
        //jQuery(function() {
        //    jQuery("#grid")
        //        .kendoGrid({
        //            "columns": [
        //            {
        //                "title": "Product name",
        //                "field": "ProductName",
        //                "filterable": {},
        //                "encoded": true,
        //                "editor": "\u003cinput class=\"k-textbox\" data-val=\"true\" data-val-required=\"The Product name field is required.\" id=\"ProductName\" name=\"ProductName\" /\u003e\u003cspan class=\"field-validation-valid\" data-valmsg-for=\"ProductName\" data-valmsg-replace=\"true\"\u003e\u003c/span\u003e"
        //            }, {
        //                "title": "Unit price",
        //                "width": "100px",
        //                "field": "UnitPrice",
        //                "format": "{0:C}",
        //                "filterable": {},
        //                "encoded": true,
        //                "editor": "\u003cinput data-val=\"true\" data-val-number=\"The field Unit price must be a number.\" data-val-range=\"The field Unit price must be between 0 and 2147483647.\" data-val-range-max=\"2147483647\" data-val-range-min=\"0\" data-val-required=\"The Unit price field is required.\" id=\"UnitPrice\" max=\"2147483647\" min=\"0\" name=\"UnitPrice\" style=\"width:100%\" type=\"number\" value=\"0\" /\u003e\u003cscript\u003e\r\n\tjQuery(function(){jQuery(\"#UnitPrice\").kendoNumericTextBox({\"format\":\"c\"});});\r\n\u003c/script\u003e\n\n\n\u003cspan class=\"field-validation-valid\" data-valmsg-for=\"UnitPrice\" data-valmsg-replace=\"true\"\u003e\u003c/span\u003e"
        //            }, {
        //                "title": "Units in stock",
        //                "width": "100px",
        //                "field": "UnitsInStock",
        //                "filterable": {},
        //                "encoded": true,
        //                "editor": "\u003cinput data-val=\"true\" data-val-number=\"The field Units in stock must be a number.\" data-val-range=\"The field Units in stock must be between 0 and 2147483647.\" data-val-range-max=\"2147483647\" data-val-range-min=\"0\" data-val-required=\"The Units in stock field is required.\" id=\"UnitsInStock\" max=\"2147483647\" min=\"-2147483648\" name=\"UnitsInStock\" style=\"width:100%\" type=\"number\" value=\"0\" /\u003e\u003cscript\u003e\r\n\tjQuery(function(){jQuery(\"#UnitsInStock\").kendoNumericTextBox({\"format\":\"n0\",\"decimals\":0});});\r\n\u003c/script\u003e\u003cspan class=\"field-validation-valid\" data-valmsg-for=\"UnitsInStock\" data-valmsg-replace=\"true\"\u003e\u003c/span\u003e"
        //            }, {
        //                "title": "Discontinued",
        //                "width": "100px",
        //                "field": "Discontinued",
        //                "filterable": {},
        //                "encoded": true,
        //                "editor": "\u003cinput class=\"check-box\" data-val=\"true\" data-val-required=\"The Discontinued field is required.\" id=\"Discontinued\" name=\"Discontinued\" type=\"checkbox\" value=\"true\" /\u003e\u003cinput name=\"Discontinued\" type=\"hidden\" value=\"false\" /\u003e\u003cspan class=\"field-validation-valid\" data-valmsg-for=\"Discontinued\" data-valmsg-replace=\"true\"\u003e\u003c/span\u003e"
        //            }, { "command": [{ "name": "destroy", "buttonType": "ImageAndText", "text": "Delete" }] }
        //            ],
        //            "pageable": { "buttonCount": 10 },
        //            "sortable": true,
        //            "filterable": true,
        //            "scrollable": false,
        //            "editable": {
        //                "confirmation": "Are you sure you want to delete this record?",
        //                "confirmDelete": "Delete",
        //                "cancelDelete": "Cancel",
        //                "mode": "incell",
        //                "template": null,
        //                "create": true,
        //                "update": true,
        //                "destroy": true
        //            },
        //            "toolbar": {
        //                "command": [
        //                    { "name": null, "buttonType": "ImageAndText", "text": "Add new record" },
        //                    { "name": null, "buttonType": "ImageAndText" }
        //                ]
        //            },
        //            "dataSource": {
        //                "type": (function() {
        //                    if (kendo.data.transports['webapi']) {
        //                        return 'webapi';
        //                    } else {
        //                        throw new Error('The kendo.aspnetmvc.min.js script is not included.');
        //                    }
        //                })(),
        //                "transport": {
        //                    "read": { "url": "/api/Product" },
        //                    "prefix": "",
        //                    "update": { "url": "/api/Product/{0}" },
        //                    "create": { "url": "/api/Product" },
        //                    "destroy": { "url": "/api/Product/{0}" },
        //                    "idField": "ProductID"
        //                },
        //                "pageSize": 10,
        //                "page": 1,
        //                "total": 0,
        //                "serverPaging": true,
        //                "serverSorting": true,
        //                "serverFiltering": true,
        //                "serverGrouping": true,
        //                "serverAggregates": true,
        //                "filter": [],
        //                "error": error_handler,
        //                "schema": {
        //                    "data": "Data",
        //                    "total": "Total",
        //                    "errors": "Errors",
        //                    "model": { "id": "ProductID", "fields": { "ProductID": { "type": "number" }, "ProductName": { "type": "string" }, "UnitPrice": { "type": "number" }, "UnitsInStock": { "type": "number" }, "Discontinued": { "type": "boolean" }, "LastSupply": { "type": "date" }, "UnitsOnOrder": { "type": "number" }, "CategoryID": { "type": "number", "defaultValue": null }, "QuantityPerUnit": { "type": "string" } } }
        //                },
        //                "batch": true
        //            }
        //        });
        //});

    }
});